'use strict';
const db = uniCloud.database()
const coll = db.collection('user')
const coll2 = db.collection('guanzhong')
const coll3 = db.collection('timu')
exports.main = async (event, context) => {
	let gongyong = await coll3.where({
		area: '公用'
	}).get()
	let ruiqi = await coll3.where({
		area: '瑞奇'
	}).get()
	let fenghe = await coll3.where({
		area: '风和'
	}).get()
	let paierte = await coll3.where({
		area: '派尔特'
	}).get()

	if (event.isUser) {
		//选手登录
		let res = await coll.where({
			name: event.name,
			// area: event.area
		}).get()
		console.log('res', res)
		if (res.data.length > 0) {
			let timuFlag = res.data[0].teamname.split('-')[0]
			console.log('timuFlag', timuFlag)
			if (timuFlag == '瑞奇') {
				let resX = await coll.where({
					name: event.name,
					// area: event.area
				}).update({
					tiku: [...gongyong.data, ...ruiqi.data]
				})
				console.log('瑞奇组的题目：', [...gongyong.data, ...ruiqi.data])
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: [...gongyong.data, ...ruiqi.data]
				}
			} else if (timuFlag == '风和') {
				let resX = await coll.where({
					name: event.name,
					// area: event.area
				}).update({
					tiku: [...gongyong.data, ...fenghe.data]
				})
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: [...gongyong.data, ...fenghe.data]
				}
			} else { //派尔特
				let resX = await coll.where({
					name: event.name,
					// area: event.area
				}).update({
					tiku: [...gongyong.data, ...paierte.data]
				})
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: [...gongyong.data, ...paierte.data]
				}
			}
		} else {
			//登录失败
			return {
				status: 0,
				msg: '账号信息错误，请重试'
			}
		}


	} else {
		//匹配成功
		//获取该成员下的题目数组
		let timuRes = await coll3.get()
		console.log('题目总数', timuRes.data)
		// let allTimu = timuRes.data //题目总数
		let allTimu = [...fenghe.data, ...paierte.data, ...ruiqi.data]
		console.log('3个分组的题目合集', allTimu)
		//随机抽取5个返回客户端
		let result = []
		let ranNum = 4;
		for (let i = 0; i < ranNum; i++) {
			let ran = Math.floor(Math.random() * (allTimu.length - i));

			result.push(allTimu[ran]);

			allTimu[ran] = allTimu[allTimu.length - i - 1];

		};
		// console.log('观众抽取的15题', result)
		let result1 = [...gongyong.data, ...result]
		console.log('观众抽取的15题', result1)
		//观众登录
		//查询是否存在
		let res = await coll2.where({
			name: event.guanzhongname,
			tel: event.guanzhongtel
		}).get()


		if (res.data.length > 0) {
			if (res.data[0].tiku.length > 0) {
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: res.data[0].tiku
				}
			} else {
				let res = await coll2.where({
					name: event.guanzhongname,
					tel: event.guanzhongtel
				}).update({
					tiku: result1
				})
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: result1
				}
			}


			//存在直接登录  登陆成功
			/* return {
				status: 1,
				msg: '登陆成功',
				data: res.data[0],
				timuarr: result
			} */
		} else {
			//不存在 创建 再返回 初始化标识 为 guanzhong
			let res1 = await coll2.add({
				name: event.guanzhongname,
				tel: event.guanzhongtel,
				flag: 'guanzhong',
				zongfen: 0,
				shijian: 0,
				status: 0,
				tiku: result1,
				tiNum: 1
			})
			console.log('res1', res1)
			let res2 = await coll2.where({
				_id: res1.id
			}).get()




			return {
				status: 1,
				msg: '登陆成功',
				data: res2.data[0],
				timuarr: res2.data[0].tiku
			}
		}


	}
};
